package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcRePunAssess;
import com.lxmes.hr.mapper.NcRePunAssessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcRePunSummaryMapper;
import com.lxmes.hr.domain.NcRePunSummary;
import com.lxmes.hr.service.INcRePunSummaryService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 奖惩汇总Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcRePunSummaryServiceImpl implements INcRePunSummaryService
{
    @Autowired
    private NcRePunSummaryMapper ncRePunSummaryMapper;

    @Autowired
    private NcRePunAssessMapper ncRePunAssessMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询奖惩汇总
     *
     * @param id 奖惩汇总主键
     * @return 奖惩汇总
     */
    @Override
    public NcRePunSummary selectNcRePunSummaryById(Long id)
    {
        return ncRePunSummaryMapper.selectNcRePunSummaryById(id);
    }

    /**
     * 查询奖惩汇总列表
     *
     * @param ncRePunSummary 奖惩汇总
     * @return 奖惩汇总
     */
    @Override
    public List<NcRePunSummary> selectNcRePunSummaryList(NcRePunSummary ncRePunSummary)
    {
        return ncRePunSummaryMapper.selectNcRePunSummaryList(ncRePunSummary);
    }

    /**
     * 新增奖惩汇总
     *
     * @param ncRePunSummary 奖惩汇总
     * @return 结果
     */
    @Override
    public int insertNcRePunSummary(NcRePunSummary ncRePunSummary)
    {
        ncRePunSummary.setCreateTime(DateUtils.getNowDate());
        return ncRePunSummaryMapper.insertNcRePunSummary(ncRePunSummary);
    }

    /**
     * 修改奖惩汇总
     *
     * @param ncRePunSummary 奖惩汇总
     * @return 结果
     */
    @Override
    public int updateNcRePunSummary(NcRePunSummary ncRePunSummary)
    {
        ncRePunSummary.setUpdateTime(DateUtils.getNowDate());
        return ncRePunSummaryMapper.updateNcRePunSummary(ncRePunSummary);
    }

    /**
     * 批量删除奖惩汇总
     *
     * @param ids 需要删除的奖惩汇总主键
     * @return 结果
     */
    @Override
    public int deleteNcRePunSummaryByIds(Long[] ids)
    {
        return ncRePunSummaryMapper.deleteNcRePunSummaryByIds(ids);
    }

    /**
     * 删除奖惩汇总信息
     *
     * @param id 奖惩汇总主键
     * @return 结果
     */
    @Override
    public int deleteNcRePunSummaryById(Long id)
    {
        return ncRePunSummaryMapper.deleteNcRePunSummaryById(id);
    }

    @Override
    public String imporIteminfo(List<NcRePunSummary> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcRePunSummary org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcRePunSummary(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int punishSummary() {
        ncRePunSummaryMapper.deleteNcRePunSummaryCurrentMonth();
        List<NcRePunSummary> ncRePunSummaries = ncRePunSummaryMapper.selectPunishGroupBy();
        for(NcRePunSummary summary : ncRePunSummaries){
            ncRePunSummaryMapper.insertNcRePunSummary(summary);
        }
        return 1;
    }
}
